package math;

/**
 * @author Jisheng Huang
 * @version 20250601
 */
public class DistributeCandiesII_2929_2 {

    public static long distributeCandies(int n, int limit) {
        return cal(n + 2) - 3 * cal(n - limit + 1) + 3 * cal(n - (limit + 2) * 2 + 2) - cal(n - 3 * (limit + 1) + 2);
    }

    public static long cal(int x) {
        if (x < 0) {
            return 0;
        }

        return (long) x * (x - 1) / 2;
    }

    public static void main(String[] args) {
        System.out.println(distributeCandies(5, 2));
        System.out.println(distributeCandies(3, 3));
        System.out.println(distributeCandies(5, 3));
    }
}
